package me.dwliu.ebase.modules.gen.controller;

import lombok.extern.slf4j.Slf4j;
import me.dwliu.ebase.modules.gen.entity.ColumnEntity;
import me.dwliu.ebase.modules.gen.service.SysGenService;
import me.dwliu.ebase.modules.gen.util.PageUtils;
import me.dwliu.ebase.modules.gen.util.Query;
import me.dwliu.ebase.modules.gen.util.Result;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

/**
 * 代码生成器Controller
 *
 * @author liudw
 * @create 2018-03-27 17:35
 **/
@Controller
@RequestMapping(value = "sys/gen")
@Slf4j
public class SysGenController {
    @Autowired
    private SysGenService sysGenService;

    /**
     * 查询列表
     *
     * @param param
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public Result queryList(@RequestParam Map<String, Object> param) {

        Query query = new Query(param);
        PageUtils pageUtils = sysGenService.queryList(query);
        Result r = Result.ok();
        r.put("totalCount", pageUtils.getTotalCount());
        r.put("list", pageUtils.getList());

        return r;
    }


    /**
     * 查询列列表
     *
     * @param tableName
     * @return
     */
    @RequestMapping("listColumn")
    @ResponseBody
    public Result queryListColumns(@RequestParam String tableName) {
        List<ColumnEntity> queryColumns = sysGenService.queryColumns(tableName);
        Result r = Result.ok();
        r.put("list", queryColumns);
        return r;
    }

    /**
     * 预览代码生成
     *
     * @param tableName 表名
     * @return
     */
    @RequestMapping("previewTemplateCode")
    @ResponseBody
    public Result previewTemplateCode(@RequestParam String tableName) {
        List<Map<String, String>> list = sysGenService.previewTemplateCode(tableName);
        Result r = Result.ok();
        r.put("list", list);
        return r;
    }

    @RequestMapping("genCode")
    public void genCode(HttpServletRequest request, HttpServletResponse response) {
        String tablesStr = request.getParameter("tables");
        String[] tableNames = null;
        if (StringUtils.isNotBlank(tablesStr) && StringUtils.contains(tablesStr, ",")) {
            tableNames = StringUtils.split(tablesStr, ",");
        }

        if (StringUtils.isNotBlank(tablesStr) && !StringUtils.contains(tablesStr, ",")) {
            tableNames = new String[]{tablesStr};
        }
        byte[] data = sysGenService.genCode(tableNames);

        String fileName = "code.zip";

        response.reset();

        try {
            response.setHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes("gb2312"), "iso8859-1"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            log.error("代码生成工具错误文件编码异常:{}", e.getMessage());
        }
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");

        try {
            IOUtils.write(data, response.getOutputStream());
        } catch (IOException e) {
            log.error("代码生成工具错误:{}", e.getMessage());
            e.printStackTrace();

        }

    }
}
